package com.mingtu.jgspatrol.activity;

import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.TimeUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.mingtu.common.adapter.MyFragmentPagerAdapter;
import com.mingtu.common.base.BaseBindingActivity;
import com.mingtu.common.utils.ActivityUtil;
import com.mingtu.common.utils.MyLogUtil;
import com.mingtu.common.utils.ScreenUtils;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.databinding.ActivityParkAuditBinding;
import com.mingtu.jgspatrol.fragment.ParkAuditFragment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class ParkAuditActivity extends BaseBindingActivity<ActivityParkAuditBinding> {

    private int checkPosition;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] dateStr = new String[]{"（今天）", "（明天）", "（后天）"};
    private String[] dateArray;
    private List<Calendar> calendars = new ArrayList<>();
    private ParkAuditFragment fragment1;
    private ParkAuditFragment fragment2;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_park_audit;
    }

    @Override
    protected void initView() {
        setModuleTitle("车辆审核");
        setDarkThemeStatusBar();
        addClickListener(this, binding.topLayout.tvText, binding.layoutPass, binding.layoutReject);

    }

    @Override
    protected void initData() {

        fragment1 = ParkAuditFragment.getInstance(1, binding.tvPassNum);
        fragment2 = ParkAuditFragment.getInstance(2, binding.tvRejectNum);

        mFragments.add(fragment1);
        mFragments.add(fragment2);

        binding.viewPager.setOffscreenPageLimit(mFragments.size());
        binding.viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        binding.viewPager.setCurrentItem(0);
        binding.viewPager.addOnPageChangeListener(new myOnPageChangeListener());

        addClickListener(this, binding.topLayout.tvText, binding.layoutPass, binding.layoutReject);

    }

    @Override
    protected void getData() {
        getDateData();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_text:
                showDatePop();
                break;
            case R.id.layout_pass:
                binding.viewPager.setCurrentItem(0);
                break;
            case R.id.layout_reject:
                binding.viewPager.setCurrentItem(1);
                break;
        }
    }

    private class myOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            setCurrentTab(position);
            MyLogUtil.e("testtest", "position===" + position);

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    private void setCurrentTab(int position) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                int primaryColor = context.getResources().getColor(R.color.colorPrimary);
                int textColor333 = context.getColor(R.color.text_color333);
                int textColorFF8 = context.getColor(R.color.text_color_ff8);

                float boldTextSize = getResources().getDimensionPixelSize(R.dimen.sp_16);
                float normalTextSize = getResources().getDimensionPixelSize(R.dimen.sp_14);

                if (position == 0) {
                    setTabStyle(binding.tvPass, binding.tvPassNum, binding.underLinePass,
                            primaryColor, primaryColor, Typeface.BOLD, boldTextSize, View.VISIBLE);

                    setTabStyle(binding.tvReject, binding.tvRejectNum, binding.underLineReject,
                            textColor333, textColorFF8, Typeface.NORMAL, normalTextSize, View.INVISIBLE);
                } else if (position == 1) {
                    setTabStyle(binding.tvPass, binding.tvPassNum, binding.underLinePass,
                            textColor333, primaryColor, Typeface.NORMAL, normalTextSize, View.INVISIBLE);

                    setTabStyle(binding.tvReject, binding.tvRejectNum, binding.underLineReject,
                            textColorFF8, textColorFF8, Typeface.BOLD, boldTextSize, View.VISIBLE);
                }
            }
        });

    }

    private void setTabStyle(TextView textView, TextView numView, View underLineView,
                             int textColor, int numColor, int typefaceStyle, float textSize, int visibility) {
        textView.setTextColor(textColor);
        numView.setTextColor(numColor);

        Typeface typeface = Typeface.create(Typeface.DEFAULT, typefaceStyle);
        textView.setTypeface(typeface);
        numView.setTypeface(typeface);

        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        numView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

        underLineView.setVisibility(visibility);
    }

    /**
     * 显示日期列表
     */
    private void showDatePop() {
        new XPopup.Builder(ActivityUtil.getTopActivity())
                .hasShadowBg(true)
                .maxHeight(ScreenUtils.getScreenHeight() / 2)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                .asCenterList("请选择日期", dateArray,
                        null, checkPosition,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                checkPosition = position;
                                binding.topLayout.tvText.setText(dateArray[position]);
                                if (position == 0) {
                                    //全部
                                    fragment1.getCurrentAuditData(null);
                                    fragment2.getCurrentAuditData(null);

                                } else {
                                    Calendar calendar = calendars.get(position - 1);
                                    Date currentDate = calendar.getTime();
                                    String currentTime = TimeUtils.date2String(currentDate, "yyyy-MM-dd");
                                    fragment1.getCurrentAuditData(currentTime);
                                    fragment2.getCurrentAuditData(currentTime);

                                }
                            }
                        }).show();

    }

    private void getDateData() {
        dateArray = new String[4];
        dateArray[0] = "全部";
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < dateStr.length; i++) {
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1;
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            dateArray[i + 1] = year + "年" + month + "月" + day + "日" + dateStr[i];
            calendars.add((Calendar) calendar.clone()); //避免所有元素引用同一个 Calendar 对象
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        binding.topLayout.tvText.setText(dateArray[0]);

    }

}